use alice2d::{
    event::{Event, EventDispatch, MouseScrollEvent, WindowResizeEvent},
    input::{KeyCode, INPUT},
    math::Vector3f,
    scene::Camera,
};

use super::EditorCamera;

pub struct OrthographicCameraController {
    camera: EditorCamera,
    aspect_ratio: f32,
    zoom_level: f32,
    rotation: bool,
    camera_position: Vector3f,
    camera_rotation: f32,
    camera_translation_speed: f32,
    camera_rotation_speed: f32,
}

impl OrthographicCameraController {
    pub fn new(aspect: f32, rotation: Option<bool>) -> Self {
        let zoom_level = 3.0;
        let camera = EditorCamera::new_with_orthographic(
            -aspect * zoom_level,
            aspect * zoom_level,
            -zoom_level,
            zoom_level,
        );
        let rotation = if let Some(rotation) = rotation {
            rotation
        } else {
            false
        };
        Self {
            camera,
            aspect_ratio: aspect,
            zoom_level,
            rotation,
            camera_position: Vector3f::ZERO,
            camera_rotation: 0.0,
            camera_translation_speed: 5.0,
            camera_rotation_speed: 180.0,
        }
    }

    pub fn get_camera(&self) -> &EditorCamera {
        &self.camera
    }

    pub fn get_camera_mut(&mut self) -> &mut EditorCamera {
        &mut self.camera
    }

    pub fn get_zoom_level(&self) -> f32 {
        self.zoom_level
    }

    pub fn set_zoom_level(&mut self, level: f32) {
        self.zoom_level = level;
    }

    pub fn mouse_scroll(&mut self, event: &mut MouseScrollEvent) -> bool {
        self.zoom_level -= event.get_wheel_y() * 0.25;
        self.zoom_level = self.zoom_level.max(0.25);

        self.camera.set_projection(
            -self.aspect_ratio * self.zoom_level,
            self.aspect_ratio * self.zoom_level,
            -self.zoom_level,
            self.zoom_level,
        );

        return false;
    }

    pub fn window_resize(&mut self, _event: &mut WindowResizeEvent) -> bool {
        return false;
    }

    pub fn tick(&mut self, dt: f64) {
        let dt = dt as f32;
        if let Some(input) = unsafe { INPUT.get() } {
            // if input.is_key_pressed(KeyCode::A) {
            //     self.camera_position.x -= self.camera_rotation.to_radians().cos() *self.camera_translation_speed * dt;
            //     self.camera_position.y -= self.camera_rotation.to_radians().sin() *self.camera_translation_speed * dt;
            // }else if input.is_key_pressed(KeyCode::D) {
            //     self.camera_position.x += self.camera_rotation.to_radians().cos() *self.camera_translation_speed * dt;
            //     self.camera_position.y += self.camera_rotation.to_radians().sin() *self.camera_translation_speed * dt;
            // }else if input.is_key_pressed(KeyCode::W) {
            //     self.camera_position.x += -self.camera_rotation.to_radians().sin() *self.camera_translation_speed * dt;
            //     self.camera_position.y += self.camera_rotation.to_radians().cos() *self.camera_translation_speed * dt;
            // }else if input.is_key_pressed(KeyCode::S) {
            //     self.camera_position.x -= -self.camera_rotation.to_radians().sin() *self.camera_translation_speed * dt;
            //     self.camera_position.y -= self.camera_rotation.to_radians().cos() *self.camera_translation_speed * dt;
            // }

            if self.rotation {
                if input.is_key_pressed(KeyCode::Q) {
                    self.camera_rotation += self.camera_rotation_speed * dt;
                }

                if input.is_key_pressed(KeyCode::E) {
                    self.camera_rotation -= self.camera_rotation_speed * dt;
                }

                if self.camera_rotation > 180.0 {
                    self.camera_rotation -= 360.0;
                } else if self.camera_rotation <= -180.0 {
                    self.camera_rotation += 360.0;
                }

                self.camera.set_rotation(self.camera_rotation);
            }
        }

        self.camera.set_position(self.camera_position);

        self.camera_translation_speed = self.zoom_level;
        // self.camera.tick();
    }

    pub fn event(&mut self, event: &mut Box<dyn Event>) {
        let mut event_dispatch = EventDispatch::new(event);

        event_dispatch.dispatch::<MouseScrollEvent, _>(|e| self.mouse_scroll(e));

        event_dispatch.dispatch::<WindowResizeEvent, _>(|e| self.window_resize(e));
    }
}
